package logic.control
{
	import common.manager.ObserverEvt;
	import common.manager.keymanager.KeyCode;
	
	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	
	import logic.def.Def;

	public class ComponentMoveControl
	{
		private var m_target : DisplayObject;
		
		private var m_vx : Number;
		private var m_vy : Number;
		
		private var m_locked : Boolean;
		
		private var m_addListenter : Boolean;
		
		public function ComponentMoveControl(targetObject : DisplayObject = null,vx:Number = 0,vy:Number = 0)
		{
			target = targetObject;
			
			m_vx = vx;
			m_vy = vy;
			
			m_locked = false;
		}
		
		public function get target() : DisplayObject
		{
			return m_target;
		}
		
		public function set target(value : DisplayObject) : void
		{
			var stage : Stage;
			
			if(value)
			{
				stage  = value.stage;
				
				if(m_target)
				{
					stage.removeEventListener(KeyboardEvent.KEY_DOWN,onKeyADown);
					stage.removeEventListener(KeyboardEvent.KEY_UP,onKeyAUp);
				}
			}
			
			m_target = value;
			
			if(!value) return;
			
			if(m_addListenter) return;
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP,onKeyUp);
		}
		
		public function destory() : void
		{
			var stage : Stage = m_target.stage;
			
			if(m_target)
			{
				stage.removeEventListener(KeyboardEvent.KEY_DOWN,onKeyADown);
				stage.removeEventListener(KeyboardEvent.KEY_UP,onKeyAUp);
			}
			
			m_target = null;
			m_vx = 0;
			m_vy = 0;
			m_addListenter = false;
		}
		
		private function onKeyDown(evt : KeyboardEvent) : void
		{
			if(m_locked) return;
			
			switch(evt.keyCode)
			{
				case KeyCode.VK_A :
					onKeyADown();
					break;
				case KeyCode.VK_S : 
					onKeySDown();
					break;
				case KeyCode.VK_D : 
					onKeyDDown();
					break;
				case KeyCode.VK_W : 
					onKeyWDown();
					break;
			}
		}
		
		private function onKeyUp(evt : KeyboardEvent) : void
		{
			
		}
		
		private function onKeyWDown() : void
		{
			if(!m_target) return;
			
			m_target.y -= m_vy;
			
			ObserverEvt.Send(Def.COMPONENTPROPERTYCHANGED);
		}
		
		private function onKeyWUp() : void
		{
			if(!m_target) return;
		}
		
		private function onKeySDown() : void
		{
			if(!m_target) return;
			
			m_target.y += m_vy;
			
			ObserverEvt.Send(Def.COMPONENTPROPERTYCHANGED);
		}
		
		private function onKeySUp() : void
		{
			if(!m_target) return;
		}
		
		private function onKeyADown() : void
		{
			if(!m_target) return;
			
			m_target.x -= m_vx;
			
			ObserverEvt.Send(Def.COMPONENTPROPERTYCHANGED);
		}
		
		private function onKeyAUp() : void
		{
			if(!m_target) return;
		}
		
		private function onKeyDDown() : void
		{
			if(!m_target) return;
			
			m_target.x += m_vx;
			
			ObserverEvt.Send(Def.COMPONENTPROPERTYCHANGED);
		}
		
		private function onKeyDUp() : void
		{
			if(!m_target) return;
		}
		
		/**
		 * 获取是否锁定
		 * @return  
		 */		
		public function get locked() : Boolean
		{
			return m_locked;
		}
		
		/**
		 * 设置是否锁定
		 * @param value 
		 */		
		public function set locked(value : Boolean) : void
		{
			m_locked = value;
		}
	}
}